import { DataResponse } from '@/services/typing';
import {
  GetUserInfoApiRes,
  UpdateUserInfoApiParams,
  UpdateUserInfoApiRes,
  UserLoginApiParams,
  UserLoginApiRes,
} from '@/services/user/typings';
import { request } from '@umijs/max';

export const userLoginApi = async (
  data: UserLoginApiParams,
): Promise<DataResponse<UserLoginApiRes>> => {
  const formData = new FormData();
  (Object.keys(data) as (keyof UserLoginApiParams)[]).forEach((key) =>
    formData.append(key as string, data[key]),
  );

  return request('/api/v1/admin/login', {
    method: 'POST',
    data: formData,
    requestType: 'form',
  });
};

export const updateUserInfoApi = async (
  data: UpdateUserInfoApiParams,
): Promise<DataResponse<UpdateUserInfoApiRes>> => {
  return request('/api/v1/admin/updateUserInfo', {
    method: 'PATCH',
    data,
  });
};

export const getUserInfoApi = async (): Promise<
  DataResponse<GetUserInfoApiRes>
> => {
  return request('/api/getUserInfo', {
    method: 'GET',
  });
};
